package org.swimv2.sessions;

import java.util.Collection;
import java.util.Date;
import java.util.Set;
import java.util.TreeSet;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.jboss.ejb3.annotation.RemoteBinding;
import org.swimv2.entities.Feedback;
import org.swimv2.entities.Job;
import org.swimv2.entities.Skill;
import org.swimv2.entities.User;
import org.swimv2.interfaces.JobsManagerRemote;

/**
 * Session Bean implementation class JobsManager
 */
@Stateless
@RemoteBinding(jndiBinding="JobsManagerJNDI")
public class JobsManager implements JobsManagerRemote {
	
	public static final String REQUESTED  = "REQUESTED";
	public static final String INPROGRESS = "IN_PROGRESS";
	public static final String CLOSED     = "CLOSED";
	
	@PersistenceContext(unitName="JPADB")
    private EntityManager manager;
	
	
	@Override
	public Job getJob(int id) {
		Job j;
		
		try {
			Query q = manager.createNamedQuery("Job.getJobById");
			q.setParameter("id", id);
			j = (Job) q.getSingleResult();
		}
		catch (Exception e) {
			return null;
		}
		return j;
	}
	
	
	@Override
	public boolean sendHelpReq(User client, User worker, String descr, String place, Skill skill, int pay, Date date) {
		try {
			
			Job j = new Job();
			
			
			User cl = manager.find(User.class, client.getId());
			
			j.setClient(cl);
			
			User wo = manager.find(User.class, worker.getId());
			j.setWorker(wo);
			
			j.setDescription(descr);
			
			j.setPlace(place);
			j.setSkill(skill);
			j.setPay(pay);
			j.setStartDate(date);
			j.setStatus(REQUESTED);
			
			manager.persist(j);
			j = manager.merge(j);
			
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		
	}
	
	@Override
	public Collection<Job> listReceivedHelpReq(User us) {
		
		User user = manager.find(User.class, us.getId());
		
		String q = "SELECT h FROM Job AS h, IN(h.worker) u WHERE u = :user AND h.status = :status"; 
		
		Query que = manager.createQuery(q);
		
		que.setParameter("user", user);
		que.setParameter("status", REQUESTED);
		
		Collection<Job> res = (Collection<Job>) que.getResultList();
		
		return res;
		
	}

	@Override
	public boolean openJob(Job job) {
		
		job = manager.merge(job);
		
		String q = "UPDATE Job j SET j.status = :status WHERE j = :job";
		
		manager.flush();

		Query que = manager.createQuery(q);
		que.setParameter("status", INPROGRESS);
		que.setParameter("job", job);
		
		int k = que.executeUpdate();
		
		return k > 0;
		
	}
	
	

	@Override
	public boolean addFeedback(Job job, Feedback feedback) {
		
		job = manager.merge(job);
		manager.flush();
				
		if (job.getFeedback() == null && job.getStatus().equals(INPROGRESS)){
			
			manager.persist(feedback);

			job.setFeedback(feedback);
		
			String q = "UPDATE Job j SET j.feedback = :feedback, j.status = :status WHERE j = :job";
			
			manager.flush();
			
			Query que = manager.createQuery(q);
			que.setParameter("feedback", feedback);
			que.setParameter("job", job);
			que.setParameter("status", CLOSED);
			
			int k = que.executeUpdate();
			
			return k > 0;
		
		}
		
		return false;
		
	}
	
	

	@Override
	public Collection<Job> listJobsWork(User us) {
		
		User user = manager.find(User.class, us.getId());
		
//		String q = "SELECT j FROM Job AS j, IN(j.worker) u WHERE u = :user AND j.status = :status";
		String q = "SELECT j FROM Job AS j, IN(j.worker) u WHERE u = :user";
		Query que = manager.createQuery(q);
		que.setParameter("user", user);
//		que.setParameter("status", INPROGRESS);
		
		Collection<Job> res = (Collection<Job>) que.getResultList();
		return res;
	}

	@Override
	public Collection<Job> listJobsClient(User us) {
		
		User user = manager.find(User.class, us.getId());
		
//		String q = "SELECT j FROM Job AS j, IN(j.client) u WHERE u = :user AND j.status = :status";
		String q = "SELECT j FROM Job AS j, IN(j.client) u WHERE u = :user";
		Query que = manager.createQuery(q);
		que.setParameter("user", user);
//		que.setParameter("status", INPROGRESS);
		
		Collection<Job> res = (Collection<Job>) que.getResultList();
		return res;
	}

	@Override
	public Collection<Job> listSentHelpReq(User us) {
		
		User user = manager.find(User.class, us.getId());
		
		String q = "SELECT h FROM Job AS h, IN(h.client) u WHERE u = :user AND h.status = :status";
		Query que = manager.createQuery(q);
		que.setParameter("user", user);
		que.setParameter("status", REQUESTED);
		
		Collection<Job> res = (Collection<Job>) que.getResultList();
		return res;
		
	}


	@Override
	public boolean removeJob(Job j) {
		
		Job job = manager.find(Job.class, j.getId());
		
		String q = "DELETE FROM Job j WHERE s = :job";
		
		manager.flush();
	
		Query que = manager.createQuery(q);
		que.setParameter("job", job);
		
		return que.executeUpdate() > 0;
		
	}



}

